<?php
class MemberAction extends Action {


	//登陆检测_前置
	public function _before_login(){
	    if (empty($_POST['username'])) {
			//$this->error(L('login_username_check'));
		}
		if (empty($_POST['password'])) {
			//$this->error(L('login_password_check'));
		}			
	}

	public function login(){

		
		
		if(IS_POST){
			if(session('verify') != md5($_POST['verify'])) {
				$this->ajaxReturn("verify","验证码填写错误！",0);
			}
			
			$where = array();
			$username = trim($_POST['username']);
			$password = trim($_POST['password']);
			$where['username'] = trim($username);
			
			$Member =   M('Member');
			$Data = $Member->where($where)->find();
			if (NULL == $Data) {
				
				$this->ajaxReturn("username","会员账号不存在！",0);
			}
			//验证用户密码
			$userpassword = md5(md5(trim($password)).$Data['encrypt']);
			
			if ($Data['password'] != $userpassword) {
				$this->ajaxReturn("password","密码输入错误！",0);
			}
			
			// 缓存访问权限
			$_SESSION[C('USER_AUTH_KEY')] = $Data['userid'];
			$_SESSION['username'] = $Data['username'];
			$this->ajaxReturn("success","恭喜您，登陆成功！",1);

		}else{
			$keys = explode(',',$seo_Arr);
			$SEO['title'] = '中国最新最全的建材求购,供应,招商,加盟信息平台-都市家建材网';
			$SEO['keywords'] = '建材,建材网,建材网站,建材新闻,建材供应,建材采购,建材招商,建材加盟,建材展会,建材企业,建材价格,建材销售,建材市场,建材产品,建材报价,建材品牌';
			$SEO['description'] = '专业提供建材产品信息,主要内容有:建材价格,建材报价,建材品牌,建材招商加盟及建材代理信息,建材商品的求购,供应,招商与合作信息;为广大建材行业的个人与企业同行们，提供海量的建材信息与商机，努力打造成国内建材行业信息免费发布平台.';
			$this->assign('SEO',$SEO);// SEO相关
			
			$this->display(); // 输出模板
		}

		

    }

	public function register(){
		if(IS_POST){
			//先把数据插入到v9_sso_members表

			
			$username = trim($_POST['username']);
			$password = trim($_POST['password']);
			$email = trim($_POST['email']);
			$nickname = trim($_POST['nickname']);
			$mobilephone = trim($_POST['mobilephone']);
			$verify = trim($_POST['verify']);
			
			//检查验证码
			if(session('verify') != md5($verify)) {
				$this->ajaxReturn("verify","验证码填写错误！",0);
			}
			//检查用户名长度
			if(strlen($username)<6 ||strlen($username)>20 ||(!$this->is_username($username))) {
				$this->ajaxReturn("username","用户已存在或禁止注册",0);
			}

			//检查密码长度
			if(strlen($password)<8 ||strlen($password)>20) {
				$this->ajaxReturn("password","密码应该为8-20位字符",0);
			}

			//检查电子邮箱
			Load('extend');
			if((!is_email($email)) || (!$this->is_email($email)) ){
				$this->ajaxReturn("email","此电子邮件被占用或格式不对.",0);
			}

			//检查公司名称
			if((!is_chinese($nickname)) || strlen($nickname) >90 || strlen($nickname) < 18 || (!$this->is_company($nickname))){
				$this->ajaxReturn("nickname","公司被注册,联系QQ:37067087.",0);
			}

			//检查手机号码
			if(11 != strlen($mobilephone)){
				$this->ajaxReturn("mobilephone","手机号码必须是11位数字.",0);
			}
			
			
			
			import("ORG.Util.String");
			$encrypt = String::randString();

			//先把注册会员数据插入到v9_sso_members表
			$SsoMembers =   M('SsoMembers');
			$SsoMembersData['username'] = $username;
			$SsoMembersData['password'] = md5(md5(trim($password)).$encrypt);
			$SsoMembersData['email'] = $email;
			$SsoMembersData['random'] = $encrypt;
			$SsoMembersData['regip'] = ip();
			$SsoMembersData['appname'] = 'phpcms v9';
			$SsoMembersData['type'] = 'app';
			$SsoMembersData['regdate'] = time();
			$SsoMembersData['lastdate'] = time();
			
			
			
			$phpssouid = $SsoMembers->add($SsoMembersData);

			//得到phpssouid后，再插入一件数据到v9_member中
			if($phpssouid){
				$Member =   M('Member');
				$MemberData['phpssouid'] = $phpssouid ;
				$MemberData['username'] = $username ;
				$MemberData['password'] = md5(md5(trim($password)).$encrypt);
				$MemberData['encrypt'] = $encrypt;
				$MemberData['nickname'] = $nickname ;
				$MemberData['email'] = $email ;
				$MemberData['regdate'] = $MemberData['lastdate'] = time();
				$MemberData['lastip'] = $MemberData['regip'] = ip();
				$MemberData['modelid'] = 10;
				$MemberData['groupid'] = 2;
				$userid = $Member->add($MemberData);
				
				//用户信息入库成功后，直接让用户登陆
				if($userid){
					$_SESSION[C('USER_AUTH_KEY')] = $userid;
					$_SESSION['username'] = $username;
					$this->success('恭喜您，注册成功。', '/');
				}else{
					
					$this->error('对不起，用户登陆失败,请尝试登陆','login');
				}
			}else{
				$this->ajaxReturn("protocol","注册失败，请稍后重试.",0);
			}

		}else{
			
			$this->display(); // 输出模板
		}
	}
	
	public function password(){
	
		$this->display(); // 输出模板
	}

	//Ajax验证用户名
	public function checkname(){
		$where = array();
		$username = trim($_GET['username']);

		if('' == $username){
			$this->ajaxReturn("username","用户名不能为空.",0);
		}

		if(strlen($username) < 6 || strlen($username) > 20 ){
			$this->ajaxReturn("username","用户名应该为6-20位之间.",0);
		}

		if ($this->is_username($username)) {
			$this->ajaxReturn("username","用户名可以注册.",1);
		}else{
			$this->ajaxReturn("username","禁止注册或用户已存在.",0);
		}
	
	}
	
	//检查用户名是否已被注册
	protected function is_username($username){
	
		$where['username'] = trim($username);
		$SsoMembers =   M('SsoMembers');
		$Data = $SsoMembers->where($where)->find();
		if (NULL == $Data) {
			return true;
		}else{
			return false;
		}
		
	}
	
	//Ajax验证电子邮件是否注册
	public function checkemail(){

		$where = array();
		$email = trim($_GET['email']);

		if('' == $email){
			$this->ajaxReturn("email","电子邮件不能为空.",0);
		}
		Load('extend');
		if(!is_email($email)){
			$this->ajaxReturn("email","电子邮件格式不正确.",0);
		}

		
		if ($this->is_email($email)) {
			$this->ajaxReturn("email","此电子邮件允许注册.",1);
		}else{
			$this->ajaxReturn("email","此电子邮件已被占用.",0);
		}
	
	}

	//检查电子邮件是否已被注册
	protected function is_email($email){
	
		$where['email'] = trim($email);
		$SsoMembers =   M('SsoMembers');
		$Data = $SsoMembers->where($where)->find();
		if (NULL == $Data) {
			return true;
		}else{
			return false;
		}
		
	}

	//Ajax验证公司名称是否注册
	public function checkcompany(){

		$where = array();
		$company = trim($_GET['nickname']);

		if('' == $company){
			$this->ajaxReturn("company","公司名称不能为空.",0);
		}

		
		if ($this->is_company($company)) {
			$this->ajaxReturn("company","公司名称允许注册.",1);
		}else{
			$this->ajaxReturn("company","公司名称已被注册.",0);
		}
	
	}

	//检查公司名称是否已被注册
	protected function is_company($nickname){
	
		$where['nickname'] = trim($nickname);
		$Member =   M('Member');
		$Data = $Member->where($where)->find();
		if (NULL == $Data) {
			return true;
		}else{
			return false;
		}
		
	}


	


	


	// 用户登出
    public function logout(){
        if (isset($_SESSION[C('USER_AUTH_KEY')])) {
			unset($_SESSION);
			session_destroy();
        }
		$this->error('您已经成功退出','login');
		
    }

	


}